<?php
/**
 * Created by 抒己社区论坛.
 * User: wjx
 * Email:978976871@qq.com
 * Date: 2018/10/11
 * Time: 10:16
 */
namespace app\admin\controller;
use think\Controller;
use app\admin\model\Admin;
use think\captcha\Captcha;
use app\admin\validate\Admin as va;
use think\Db;
use think\facade\Session;

class Login extends Controller{
    
    public function index(){
//        for($i=0;$i<3;$i++){
//            $user           = new Admin;
//            $user->name     = 'thinkphp'.$i;
//            $user->password = '123';
//            $user->phone    = 1521651122 . $i;
//            $user->save();
//            echo $user->id;echo '<br>';
//            echo $i;echo '<br>'.'----------'.'<br>';
//        }
//        dump(db::name('admin')->select());
//        echo password_hash(123,PASSWORD_DEFAULT);
        return $this->fetch();
    }
    /**
     * 用户后台登录
     */
    public function login(){
        $data = input('post.');
        $validate = new va;
        if($validate->scene('add')->check($data)){
            $admin = db::name('Admin')->where('name',$data['username'])->find();
            $verify = password_verify($data['password'],$admin['password']);
            if($verify){
                $sdata=[
                    'id'=>$admin['id'],
                    'name'=>$admin['name'],
                    'expire'=>time()+config('app.session__config.session_expire'),
                ];//expire session过期时间
                session(config('app.session__config.session_info'),$sdata);
                session(config('app.session__config.session_sign'),user_sign($sdata));//生成签名写入session
                
                return VV(true,'登陆成功');
            }else{
                return VV(false,'用户名或密码错误');
            }
        }else{
            return VV(false,$validate->getError());
        }
    }
}